﻿using NTQOnlineExamFrontend.Models.Mobile;
using NTQOnlineExamFrontend.Security;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebMatrix.WebData;
using NTQOnlineExamFrontend.Helpers;

namespace NTQOnlineExamFrontend.Api
{
    public class AuthenticationController : ApiController
    {
        [HttpGet]
        public SignInResponseModel SignIn(string username, string password)
        {
            var response = new SignInResponseModel();
            try
            {
                if (WebSecurity.Login(username, password, true))
                {
                    response.success = true;
                    Token token = new Token(username, Request.GetClientIP());
                    response.token = token.Encrypt();
                }
                else
                {
                    response.success = false;
                    response.errors = "Username or password is incorrect!";
                }
            }
            catch (Exception ex)
            {
                response.success = false;
                response.errors = ex.Message;
            }
            return response;
        }
    }
}
